package com.wslds.blog.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedOutput;
import com.wslds.base.action.BaseAction;
import com.wslds.base.dao.page.PageAdapter;
import com.wslds.base.util.BlogCons;
import com.wslds.base.util.FunctionUtil;
import com.wslds.base.util.SessionUtil;
import com.wslds.base.util.Strings;
import com.wslds.bean.Article;
import com.wslds.bean.Tag;
import com.wslds.bean.Taginst;
import com.wslds.bean.Type;
import com.wslds.bean.ext.ArticleExt;
import com.wslds.bean.ext.TaginstExt;
import com.wslds.blog.service.ArticleService;

@SuppressWarnings("serial")
@Component("articleAction")
@Scope("prototype")
public class ArticleAction extends BaseAction{

	private Article article;
	private ArticleExt articleExt;
	private List<Taginst> taginst;
	
	@Resource
	private ArticleService articleService;
	
	public String sidebar(){
		
		return SUCCESS;
	}
	
	public String listArticle(){
		
		this.setQueryString(this.getHttpRequest().getQueryString());
		return SUCCESS;
	}
	@SuppressWarnings("all")
	public String listArticleWeb(){
		
		this.setQueryString(this.getHttpRequest().getQueryString());
		
		Map<Long,Type> typeMap = (Map<Long,Type>)SessionUtil.getGlobe(BlogCons.APPLICATION.TYPE);
		Map<Long,String> tagMap = (Map<Long,String>)SessionUtil.getGlobe(BlogCons.APPLICATION.TAG);
		try{
			if( null != articleExt){
				if( null != articleExt.getTypeId() ){
					this.put("titleApart", "类型:"+typeMap.get(articleExt.getTypeId()).getName()+" - ");
				}else if( Strings.notNull(articleExt.getTags()) ){
						this.put("titleApart", "标签:"+tagMap.get(Long.valueOf(articleExt.getTags()))+" - ");
				}else if( Strings.notNull(articleExt.getDates()) ){
						this.put("titleApart", "日期:"+articleExt.getDates()+" - ");
				}
			}
		}catch( NumberFormatException e){
			return "error404";
		}
		return SUCCESS;
	}
	@SuppressWarnings("all")
	public String listAjaxArticle(){
		
		if( null == articleExt ){
			articleExt = new ArticleExt();
		}
		articleExt.setExtraOrderColumns(" id desc ");
		
		if( this.getHttpRequest().getQueryString() != null ){
			this.setQueryString(Strings.cutPageQueryString(this.getHttpRequest().getQueryString()));
		}

		Map<Long,List<TaginstExt>> tagMap = (Map<Long,List<TaginstExt>>)SessionUtil.getGlobe(BlogCons.APPLICATION.TAGINST);
		Map<Long,Type> typeMap = (Map<Long,Type>)SessionUtil.getGlobe(BlogCons.APPLICATION.TYPE);
		
		
		PageAdapter<ArticleExt> adapter = articleService.getDao().pageExt("article_select_ext", articleExt, getPage());
		this.put("adapter", adapter);
		
		for( ArticleExt ext: adapter.getData() ){
			ext.setTagList(tagMap.get(ext.getId()));
			ext.setType(typeMap.get(ext.getTypeId()));
			
			String pureData = FunctionUtil.Html2Text(ext.getContent());
			
			int blogStart = ext.getContent().indexOf(BlogCons.BLOG_SPLIT.START);
			int blogEnd = ext.getContent().indexOf(BlogCons.BLOG_SPLIT.END);
			if( blogStart == -1 || blogEnd == -1 ){
				ext.setContent(FunctionUtil.abbreviate(pureData, 500, "..."));
			}else{
				ext.setContent(ext.getContent().substring(blogStart, blogEnd)+"........");
			}
		}
		
		return SUCCESS;
	}
	@SuppressWarnings("all")
	public String listAjaxArticleWeb(){
		
		if( null == articleExt ){
			articleExt = new ArticleExt();
		}
		articleExt.setStat("MEN");
		articleExt.setExtraOrderColumns(" id desc ");
		
		if( this.getHttpRequest().getQueryString() != null ){
			this.setQueryString(Strings.cutPageQueryString(this.getHttpRequest().getQueryString()));
		}
		
		if( Strings.notNull(articleExt.getTags()) ){
			try{
				Long.valueOf(articleExt.getTags());
			}catch( NumberFormatException e){
				return "error404";
			}
		}
		
		Map<Long,List<TaginstExt>> taginstMap = (Map<Long,List<TaginstExt>>)SessionUtil.getGlobe(BlogCons.APPLICATION.TAGINST);
		Map<Long,Type> typeMap = (Map<Long,Type>)SessionUtil.getGlobe(BlogCons.APPLICATION.TYPE);
		
		PageAdapter<ArticleExt> adapter = articleService.getDao().pageExt("article_select_ext", articleExt, getPage());
		this.put("adapter", adapter);
		
		for( ArticleExt ext: adapter.getData() ){
			ext.setTagList(taginstMap.get(ext.getId()));
			ext.setType(typeMap.get(ext.getTypeId()));
			
			String pureData = FunctionUtil.Html2Text(ext.getContent());
			
			int blogStart = ext.getContent().indexOf(BlogCons.BLOG_SPLIT.START);
			int blogEnd = ext.getContent().indexOf(BlogCons.BLOG_SPLIT.END);
			if( blogStart == -1 || blogEnd == -1 ){
				ext.setContent(FunctionUtil.abbreviate(pureData, 500, "..."));
			}else{
				ext.setContent(ext.getContent().substring(blogStart, blogEnd)+"........");
			}
		}
		
		return SUCCESS;
	}
	@SuppressWarnings("all")
	public String rss() throws Exception{
		
		if( null == articleExt ){
			articleExt = new ArticleExt();
		}
		articleExt.setStat("MEN");
		articleExt.setExtraOrderColumns(" id desc ");
		
		if( this.getHttpRequest().getQueryString() != null ){
			this.setQueryString(Strings.cutPageQueryString(this.getHttpRequest().getQueryString()));
		}
		
		Map<Long,List<TaginstExt>> taginstMap = (Map<Long,List<TaginstExt>>)SessionUtil.getGlobe(BlogCons.APPLICATION.TAGINST);
		Map<Long,Type> typeMap = (Map<Long,Type>)SessionUtil.getGlobe(BlogCons.APPLICATION.TYPE);
		
		PageAdapter<ArticleExt> adapter = articleService.getDao().pageExt("article_select_ext", articleExt, getPage(),100);
		this.put("adapter", adapter);
		
		for( ArticleExt ext: adapter.getData() ){
			ext.setTagList(taginstMap.get(ext.getId()));
			ext.setType(typeMap.get(ext.getTypeId()));
		}
		
		SyndFeed feed = Strings.genRss(adapter.getData());
		this.getResponse().setContentType("text/xml");
		this.getResponse().setCharacterEncoding("UTF-8");
		PrintWriter pw = this.getResponse().getWriter();
		SyndFeedOutput output = new SyndFeedOutput();
		output.output(feed, pw);
		
		return NONE;
	}
	@SuppressWarnings("all")
	public String findArticle(){
		
		if( Strings.notNull(articleExt.getIds()) ){
			
			try{
				articleExt.setId(Long.valueOf(articleExt.getIds()));
			}catch(Exception e){
				//要处理
				return "index";
			}
			
			articleExt = articleService.getDao().findOneExt("article_select_ext", articleExt);
			
			String pureData = FunctionUtil.Html2Text(articleExt.getContent());
			articleExt.setContent4Simple(FunctionUtil.abbreviate(pureData, 150, "..."));
			
			Map<Long,List<TaginstExt>> tagMap = (Map<Long,List<TaginstExt>>)SessionUtil.getGlobe(BlogCons.APPLICATION.TAGINST);
			Map<Long,Type> typeMap = (Map<Long,Type>)SessionUtil.getGlobe(BlogCons.APPLICATION.TYPE);
			articleExt.setTagList(tagMap.get(articleExt.getId()));
			articleExt.setType(typeMap.get(articleExt.getTypeId()));
			
			List<ArticleExt> neiList = articleService.getNei(articleExt);
			this.put("neiList", neiList);
			
		}else{
			//错误去处
			return "index";
		}
		
		
		
		return SUCCESS;
	}
	@SuppressWarnings("all")
	public String findArticleWeb(){
		
		if( null == articleExt ){
			return "index";
		}
		
		if( Strings.notNull(articleExt.getIds()) ){
			articleExt.setStat("MEN");
			articleExt.setId(Long.valueOf(articleExt.getIds()));
			
			articleExt = articleService.getDao().findOneExt("article_select_ext", articleExt);
			
			if( null == articleExt ){
				return "err";
			}else{
				articleExt.setViewSum(articleExt.getViewSum()+1);
				articleService.getDao().update("article", articleExt);
			}
			
			String pureData = FunctionUtil.Html2Text(articleExt.getContent());
			articleExt.setContent4Simple(FunctionUtil.abbreviate(pureData, 150, "..."));
			
			Map<Long,List<TaginstExt>> tagMap = (Map<Long,List<TaginstExt>>)SessionUtil.getGlobe(BlogCons.APPLICATION.TAGINST);
			Map<Long,Type> typeMap = (Map<Long,Type>)SessionUtil.getGlobe(BlogCons.APPLICATION.TYPE);
			articleExt.setTagList(tagMap.get(articleExt.getId()));
			articleExt.setType(typeMap.get(articleExt.getTypeId()));
			
			List<ArticleExt> neiList = articleService.getNeiWeb(articleExt);
			this.put("neiList", neiList);
			
		}else{
			//错误去处
			return "err";
		}
		
		
		
		return SUCCESS;
	}
	
	public String insert(){
		article.setCreateDate(new Date());
		article.setModDate(new Date());
		article.setStat(BlogCons.BLOG_STAT.MEI);
		article.setViewSum(0L);
		articleService.getDao().save("article", article);
		
		if( null != taginst ){
			for( Taginst inst: taginst ){
				if( 0 != inst.getTagId()){
					inst.setArtId(article.getId());
					articleService.getDao().save("taginst", inst);
				}
			}
		}
		
		SessionUtil.refreshTag();
		SessionUtil.refreshSidebar();
		
		return SUCCESS;
	}
	
	public String toInsert(){
		List<Type> typeList = articleService.getDao().find("type", null);
		this.put("typeList", typeList);
		
		List<Tag> tagList = articleService.getDao().find("tag", null);
		this.put("tagList", tagList);
		
		return SUCCESS;
	}
	@SuppressWarnings("all")
	public String toUpdate(){
		article = articleService.getDao().findOne("article", article);
		
		Map<Long,List<TaginstExt>> tagMap = (Map<Long,List<TaginstExt>>)SessionUtil.getGlobe(BlogCons.APPLICATION.TAGINST);
		List<TaginstExt> taginstList = tagMap.get(article.getId());
		if( null == taginstList ){
			taginstList = new ArrayList<TaginstExt>();
			taginstList.add(new TaginstExt());
			taginstList.add(new TaginstExt());
			taginstList.add(new TaginstExt());
		}else{
			for( int i=taginstList.size(); i < 3; i++ ){
				taginstList.add(new TaginstExt());
			}
		}
		this.put("taginstList", tagMap.get(article.getId()));
		
		List<Type> typeList = articleService.getDao().find("type", null);
		this.put("typeList", typeList);
		
		List<Tag> tagList = articleService.getDao().find("tag", null);
		this.put("tagList", tagList);
		
		return SUCCESS;
	}
	
	public String update(){
		
		articleService.getDao().update("article", article);
		
		Taginst delTaginst = new Taginst();
		delTaginst.setArtId(article.getId());
		articleService.getDao().delete("taginst", delTaginst);
		if( null != taginst ){
			for( Taginst inst: taginst ){
				if( 0 != inst.getTagId()){
					inst.setArtId(article.getId());
					articleService.getDao().save("taginst", inst);
				}
			}
		}
		
		SessionUtil.refreshTag();
		SessionUtil.refreshSidebar();
		
		return SUCCESS;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public ArticleService getArticleService() {
		return articleService;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	public List<Taginst> getTaginst() {
		return taginst;
	}

	public void setTaginst(List<Taginst> taginst) {
		this.taginst = taginst;
	}

	public ArticleExt getArticleExt() {
		return articleExt;
	}

	public void setArticleExt(ArticleExt articleExt) {
		this.articleExt = articleExt;
	}



}
